<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        :root {
            --box-width: 800px
        }

        body {
            color: #333;
        }

        .table {
            width: var(--box-width);
            margin: auto;
            border: 1px solid #0a58e9;
        }

        .row {
            display: flex;
            border-bottom: 1px solid #0a58e9;
        }

        .row:last-child {
            border-bottom: 0px none;
        }

        .row .cel:nth-child(-n + 3) {
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .cel {
            border-right: 1px solid #0a58e9;
            min-width: 100px;
            padding: 10px;
        }

        .cel:last-child {
            border-right: 0 none;
            flex: 1;

        }

        img {
            width: 70px;
            height: 70px;
            border-radius: 5px;
        }

        .tr {
            text-align: center;
        }

        .name {
            text-align: center;
        }

        .desc {
            max-height: 150px;
            overflow: hidden;
            display: -webkit-box;
            -webkit-box-orient: vertical;
            -webkit-line-clamp: 5;
            text-overflow: ellipsis;
        }

        .total {
            width: var(--box-width);
            margin: auto;
            padding: 20px;
        }
    </style>
    <script src="./data/pvpitem.js"></script>
</head>

<body>
    <div class="total">
        <div>总价：<span>0</span></div>
    </div>
    <div class="table">
        <div class="row tr">
            <div class="cel">
                <input type="checkbox" id="all">
                <span class="all-text">全选</span>
            </div>
            <div class="cel">
                装备
            </div>
            <div class="cel">
                售价
            </div>
            <div class="cel">
                描述
            </div>
        </div>
    </div>
    <script>
        var datas = pvpitems.sort(() => Math.random() - 0.5).sort(() => Math.random() - 0.5).sort(() => Math.random() - 0.5).sort(() => Math.random() - 0.5).slice(0, 5);
        console.log(datas);
        var table = document.querySelector('.table')

        var template = '';
        datas.forEach(item => {
            template += `<div class="row">
            <div class="cel">
                <input type="checkbox" class="single">
            </div>
            <div class="cel">
                <div>
                    <img src="./images/checked/${item.item_id}.jpg" />
                    <p class="name">${item.item_name}</p>
                </div>
            </div>
            <div class="cel">
                ${item.total_price}
            </div>
            <div class="cel">
                <div class="desc">
                    ${item.des1 ? item.des1 : ''}
                    ${item.des2 ? item.des2 : ''}    
                </div>
            </div>
        </div>`
        })

        let parent = document.createElement('div')
        parent.innerHTML = template;
        let children = parent.children;
        for (let i = 0; i < children.length; i++) {
            let node = children[i].cloneNode(true);
            table.append(node)
        }

        var all = document.getElementById('all');
        var allText = document.querySelector('.all-text')
        var boxs = document.querySelectorAll('.single');
        let totalPrice = document.querySelector('.total span')
        all.onclick = function () {
            boxs.forEach(item => item.checked = all.checked)
            allText.innerText = all.checked ? '全不选' : '全选';
            calcPrice();
        }

        boxs.forEach(item => {
            item.onclick = function () {
                var isCheckAll = Array.from(boxs).every(item => item.checked)
                all.checked = isCheckAll;
                calcPrice();
            }
        })

        function calcPrice() {
            var total = Array.from(boxs).reduce((prev, cur, index) => prev += cur.checked ? datas[index].total_price : 0, 0)
            totalPrice.innerText = total;
        }
    </script>
</body>

</html>